package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.service.ShoppingCarService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //查询购物车
    @Override
    public List<ShoppingCart> list(){
        return shoppingCarMapper.list();
    }

    //添加购物车
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO){
        //查询当前菜品是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置用户id,通过BaseContext获取
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询
        List<ShoppingCart> listsp = shoppingCarMapper.listsp(shoppingCart);
        //如果在，数量加一
        if(listsp != null && listsp.size() > 0) {
            ShoppingCart cart = listsp.get(0);//获取购物车对象
            cart.setNumber(cart.getNumber() + 1);
            //更新数据
            shoppingCarMapper.updateqt(cart);
        }

        //如果不在，添加到购物车，数量默认一
                //如果是菜品，在菜品表里面查询：价格，图片
                //如果是套餐，在套餐表里面查询：价格，图片
        else {
            //判断是菜品还是套餐

            Long dishId = shoppingCart.getDishId();
            if(dishId != null){
                //添加的是菜品，查询菜品表
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                //shoppingCarMapper.insert(shoppingCart);

            }else {
                //添加的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
               // shoppingCarMapper.insert(shoppingCart);
            }
            shoppingCarMapper.insert(shoppingCart);
        }
    }

    //更新购物车商品数量
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //查询当前菜品是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置用户id,通过BaseContext获取
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("gengx购物车的servic"+shoppingCart.getId());
        //查询
        List<ShoppingCart> listsp = shoppingCarMapper.listsp(shoppingCart);
        //如果在，数量加一
        if(listsp != null && listsp.size() > 0) {
            ShoppingCart cart = listsp.get(0);//获取购物车对象
            cart.setNumber(cart.getNumber() - 1);
            //更新数据
            shoppingCarMapper.updateqt(cart);
        }

    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        log.info("清空购物车的servic"+userId);
        shoppingCarMapper.clean(userId);
    }

}
